﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BattleforgeAssault
{
    /// <summary>
    /// This class is a layout panel that will place each of its children evenly around its center.
    /// </summary>
    public class RadialPanel : Panel
    {
        #region Properties

        /// <summary>
        /// Gets or sets the starting angle, measured in degrees counterclockwise from (1,0), for the layout.
        /// </summary>
        public double StartingAngle
        {
            get { return (double)GetValue(StartingAngleProperty); }
            set { SetValue(StartingAngleProperty, value); }
        }
        public static readonly DependencyProperty StartingAngleProperty =
            DependencyProperty.Register("StartingAngle", typeof(double), typeof(RadialPanel), new PropertyMetadata(90.0));

        /// <summary>
        /// Gets or sets a value indicating whether the elements should stay in a circle or be allowed to stretch to fit.
        /// </summary>
        public bool IsEquidistant
        {
            get { return (bool)GetValue(IsEquidistantProperty); }
            set { SetValue(IsEquidistantProperty, value); }
        }
        public static readonly DependencyProperty IsEquidistantProperty =
            DependencyProperty.Register("IsEquidistant", typeof(bool), typeof(RadialPanel), new PropertyMetadata(false));

        #endregion Properties

        #region Layout Overrides

        protected override Size MeasureOverride(Size availableSize)
        {
            var maxChildSize = new Size(0.0, 0.0);

            foreach (FrameworkElement child in Children)
            {
                child.Measure(availableSize);
                maxChildSize.Height = Math.Max(maxChildSize.Height, child.DesiredSize.Height);
                maxChildSize.Width = Math.Max(maxChildSize.Width, child.DesiredSize.Width);
            }

            return maxChildSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (Children.Count == 0) { return finalSize; }

            // degrees converted to radian by multiplying with PI/180
            double angle = StartingAngle * (Math.PI / 180);
            double incrementalAngularSpace = (360.0 / Children.Count) * (Math.PI / 180);

            foreach (UIElement elem in Children)
            {
                double radiusX = finalSize.Width / 2 - (elem.DesiredSize.Width / 2);
                double radiusY = finalSize.Height / 2 - (elem.DesiredSize.Height / 2);
                if (IsEquidistant)
                {
                    radiusX = Math.Min(radiusX, radiusY);
                    radiusY = Math.Min(radiusX, radiusY);
                }

                Point childPoint = new Point(Math.Cos(angle) * radiusX, -Math.Sin(angle) * radiusY);
                Point actualChildPoint = new Point(finalSize.Width / 2 + childPoint.X - elem.DesiredSize.Width / 2,
                    finalSize.Height / 2 + childPoint.Y - elem.DesiredSize.Height / 2);

                elem.Arrange(new Rect(actualChildPoint.X, actualChildPoint.Y, elem.DesiredSize.Width, elem.DesiredSize.Height));

                angle += incrementalAngularSpace;
            }

            return finalSize;
        }
        #endregion Layout Overrides
    } // RadialPanel class
} // Namespace
